/*
 * @: 
 * @Version: 1.0
 * @Autor: ziwei
 * @Date: 2021-06-28 09:26:41
 * @LastEditors: ziwei
 * @LastEditTime: 2022-02-11 14:43:04
 */
const { AsyncSeriesWaterfallHook } = require('tapable');
const hook = new AsyncSeriesWaterfallHook(['name', 'age']);

//异步注册 全局任务完成后执行回调 Promise.all
console.time('cost');
hook.tapPromise('1', (name, age, callback) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('1',name,age);
      resolve('result1');
    }, 1000);
  });
});
hook.tapPromise('2', (name, age, callback) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('2',name,age);
      //如果有返回值了，后面的事件函数就补在执行，直接结束了
      resolve('result2');
    }, 2000);
  });
});
hook.tapPromise('3', (name, age, callback) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('3',name,age);
      resolve('result3');
    }, 3000);
  });
});

hook.promise('xiayu','12').then((result) => {
  console.log(result);
  console.timeEnd('cost');
});
